/*
 * QuarkPlayer, a Phonon media player
 * Copyright (C) 2008  Tanguy Krotoff <tkrotoff@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ScreenSaver.h"

#include <QtCore/QDebug>

#ifdef Q_OS_WIN
	#include <windows.h>

	int _lowPowerTimeout = 0;
	int _powerOffTimeout = 0;
	int _screenSaverTimeout = 0;
	bool _stateSaved = false;
#endif	//Q_OS_WIN

void ScreenSaver::disable() {
#ifdef Q_OS_WIN
	if (!_stateSaved) {
		SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT, 0, &_lowPowerTimeout, 0);
		SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0, &_powerOffTimeout, 0);
		SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &_screenSaverTimeout, 0);
		_stateSaved = true;
	}

	qDebug() << __FUNCTION__ << "lowPowerTimeout:" << _lowPowerTimeout;
	qDebug() << __FUNCTION__ << "powerOffTimeout:" << _powerOffTimeout;
	qDebug() << __FUNCTION__ << "screenSaverTimeout:" << _screenSaverTimeout;

	SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
	SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
	SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
#endif	//Q_OS_WIN
}

void ScreenSaver::restore() {
	qDebug() << __FUNCTION__;

#ifdef Q_OS_WIN
	if (_stateSaved) {
		SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, _lowPowerTimeout, NULL, 0);
		SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, _powerOffTimeout, NULL, 0);
		SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, _screenSaverTimeout, NULL, 0);
	} else {
		qCritical() << __FUNCTION__ << "Error: screensaver cannot be restored";
	}
#endif	//Q_OS_WIN
}
